﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows.Input;
using CLVisionCameraControl;
using MvCameraControl;

namespace CLVisionCameraNC620.Dialog
{
    public partial class ShowCameraSettingDialog : Form
    {
        bool isGrabbing = false;
        ICLVisionCamera device;
        public ShowCameraSettingDialog(ICLVisionCamera iCLVisionCamera)
        {
            InitializeComponent();
            this.device = iCLVisionCamera;
        }

        private void ShowCameraSettingDialog_Load(object sender, EventArgs e)
        {
            SetCtrlWhenOpen();
            // ch:获取参数 | en:Get parameters
            bnGetParam_Click(null, null);
            // device.EventGrabBackBitmapImage += (s) => { pictureBox1.Image = s; };

        }
        private void SetCtrlWhenOpen()
        {
            bnStartGrab.Enabled = true;
            bnStopGrab.Enabled = false;

            bnContinuesMode.Enabled = true;
            bnContinuesMode.Checked = true;

            bnTriggerMode.Enabled = true;
            cbSoftTrigger.Enabled = false;
            bnTriggerExec.Enabled = false;

            tbExposure.Enabled = true;
            tbGain.Enabled = true;
            tbFrameRate.Enabled = true;
            cbPixelFormat.Enabled = true;
            bnGetParam.Enabled = true;
            bnSetParam.Enabled = true;
        }

        private void bnContinuesMode_CheckedChanged(object sender, EventArgs e)
        {
            if (bnContinuesMode.Checked)
            {
                device.SetTriggerMode(EnumTriggerMode.Off, out int ErrorCode, out string ErrorText);   //Parameters.SetEnumValueByString("TriggerMode", "Off");
                cbSoftTrigger.Enabled = false;
                bnTriggerExec.Enabled = false;
            }
        }

        private void bnTriggerMode_CheckedChanged(object sender, EventArgs e)
        {
            // ch:打开触发模式 | en:Open Trigger Mode
            if (bnTriggerMode.Checked)
            {
                device.SetTriggerMode(EnumTriggerMode.On, out int ErrorCode, out string ErrorText);

                // ch:触发源选择:0 - Line0; | en:Trigger source select:0 - Line0;
                //           1 - Line1;
                //           2 - Line2;
                //           3 - Line3;
                //           4 - Counter;
                //           7 - Software;
                if (cbSoftTrigger.Checked)
                {
                    device.SetTriggerSource(EnumTriggerSource.Software, out ErrorCode, out ErrorText);
                    //  device.Parameters.SetEnumValueByString("TriggerSource", "Software");
                    if (isGrabbing)
                    {
                        bnTriggerExec.Enabled = true;
                    }
                }
                else
                {
                    device.SetTriggerSource(EnumTriggerSource.Line0, out ErrorCode, out ErrorText);
                }

            }
        }

        private void bnStartGrab_Click(object sender, EventArgs e)
        {
            device.StartGrab(out int ErrorCode, out string ErrorText);
            if (ErrorCode == -1)
            {
                MessageBox.Show(ErrorText);
                return;
            }
            isGrabbing = true;

            device.AcquireImageContinuous(pictureBox1.Handle, out ErrorCode, out ErrorText);
            if (ErrorCode == -1)
            {
                MessageBox.Show(ErrorText);
                return;
            }

            // ch:控件操作 | en:Control Operation
            SetCtrlWhenStartGrab();
        }
        private void SetCtrlWhenStartGrab()
        {
            bnStartGrab.Enabled = false;
            cbPixelFormat.Enabled = false;
            bnStopGrab.Enabled = true;

            bnContinuesMode.Enabled = false;
            bnTriggerMode.Enabled = false;

            if (bnTriggerMode.Checked)
            {
                cbSoftTrigger.Enabled = true;
            }
        }

        private void bnStopGrab_Click(object sender, EventArgs e)
        {
            device.StopGrab(out int ErrorCode, out string ErrorText);
            if (ErrorCode == -1)
            {
                MessageBox.Show(ErrorText);
            }
            // ch:控件操作 | en:Control Operation
            SetCtrlWhenStopGrab();
        }
        private void SetCtrlWhenStopGrab()
        {
            bnStartGrab.Enabled = true;
            cbPixelFormat.Enabled = true;
            bnStopGrab.Enabled = false;

            bnContinuesMode.Enabled = true;
            bnTriggerMode.Enabled = true;

            bnTriggerExec.Enabled = false;
            cbSoftTrigger.Checked = false;
        }

        private void cbSoftTrigger_CheckedChanged(object sender, EventArgs e)
        {
            if (cbSoftTrigger.Checked)
            {
                // ch:触发源设为软触发 | en:Set trigger source as Software
                device.SetTriggerSource(EnumTriggerSource.Software, out int ErrorCode, out string ErrorText);
                bnTriggerExec.Enabled = true;
            }
            else
            {
                device.SetTriggerSource(EnumTriggerSource.Line0, out int ErrorCode, out string ErrorText);
                //device.Parameters.SetEnumValueByString("TriggerSource", "Line0");
                bnTriggerExec.Enabled = false;
            }
        }

        private void bnTriggerExec_Click(object sender, EventArgs e)
        {
            // ch:触发命令 | en:Trigger command
            device.TriggerSoftwareOnce(out int ErrorCode, out string ErrorText);

        }

        private void cbPixelFormat_SelectionChangeCommitted(object sender, EventArgs e)
        {
            EnumPixelFormat pixelFormat = (EnumPixelFormat)Enum.Parse(typeof(EnumPixelFormat), cbPixelFormat.Text);
            device.SetParamPixelFormat(pixelFormat, out int ErrorCode, out string ErrorText);
        }

        private void bnGetParam_Click(object sender, EventArgs e)
        {
            GetTriggerMode();

            float floatValue;
            //int result = device.Parameters.GetFloatValue("ExposureTime", out floatValue);
            device.GetParamExposure(out floatValue, out int ErrorCode, out string ErrorText);

            tbExposure.Text = floatValue.ToString("F1");


            // result = device.Parameters.GetFloatValue("Gain", out floatValue);
            device.GetParamGain(out floatValue, out ErrorCode, out ErrorText);

            tbGain.Text = floatValue.ToString("F1");


            // result = device.Parameters.GetFloatValue("ResultingFrameRate", out floatValue);
            device.GetParamFrameRate(out floatValue, out ErrorCode, out ErrorText);

            tbFrameRate.Text = floatValue.ToString("F1");


            cbPixelFormat.Items.Clear();

            // result = device.Parameters.GetEnumValue("PixelFormat", out enumValue);
            device.GetParamPixelFormat(out EnumPixelFormat pixelFormat, out ErrorCode, out ErrorText);

            List<string> list = new List<string>() {
                "Mono10",
                "Mono12",
                "RGB8Packed",
                "BGR8Packed",
                "YUV422_YUYV_Packed",
                "YUV422Packed",
                "BayerBG8",
                "BayerBG10",
                "BayerBG10Packed",
                "BayerBG12",
                "BayerBG12Packed"
            };

            foreach (var item in list)
            {
                cbPixelFormat.Items.Add(item);
                if (item == pixelFormat.ToString())
                {
                    cbPixelFormat.SelectedIndex = cbPixelFormat.Items.Count - 1;
                }
            }


        }

        private void bnSetParam_Click(object sender, EventArgs e)
        {
            try
            {
                float.Parse(tbExposure.Text);
                float.Parse(tbGain.Text);
                float.Parse(tbFrameRate.Text);
            }
            catch
            {
                MessageBox.Show("Please enter correct type!");
                return;
            }

            // device.Parameters.SetEnumValue("ExposureAuto", 0);
            // device.SetParamExposure(2000, out _, out _);



            //int result = device.Parameters.SetFloatValue("ExposureTime", float.Parse(tbExposure.Text));
            device.SetParamExposure(float.Parse(tbExposure.Text), out int ErrorCode, out string ErrorText);


            //device.Parameters.SetEnumValue("GainAuto", 0);
            // result = device.Parameters.SetFloatValue("Gain", float.Parse(tbGain.Text));
            device.SetParamGain(float.Parse(tbGain.Text), out ErrorCode, out ErrorText);

            //result = device.Parameters.SetBoolValue("AcquisitionFrameRateEnable", true);




            //  result = device.Parameters.SetFloatValue("AcquisitionFrameRate", float.Parse(tbFrameRate.Text));

            device.SetParamFrameRate(float.Parse(tbFrameRate.Text), out ErrorCode, out ErrorText);



        }

        private void GetTriggerMode()
        {
            device.GetTriggerMode(out EnumTriggerMode enumTriggerMode, out int ErrorCode, out string ErrorText);
            //  int result = device.Parameters.GetEnumValue("TriggerMode", out enumValue);

            if (enumTriggerMode.ToString() == "On")
            {
                bnTriggerMode.Checked = true;
                bnContinuesMode.Checked = false;
                device.GetTriggerSource(out EnumTriggerSource enumTriggerSource, out ErrorCode, out ErrorText);
                if (enumTriggerSource.ToString() == "TriggerSoftware")
                {
                    cbSoftTrigger.Enabled = true;
                    cbSoftTrigger.Checked = true;
                }
            }
            else
            {
                bnContinuesMode.Checked = true;
                bnTriggerMode.Checked = false;
            }
        }

        private void ShowCameraSettingDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            device.HWnd = IntPtr.Zero;
        }
    }
}
